import uix
import uiutil
import xtriui
import util
import uiconst
import uicls

class InvItem(uicls.Container):
    __guid__ = 'xtriui.InvItem'
    __groups__ = []
    __categories__ = []
    __notifyevents__ = ['ProcessSessionChange',
     'OnSessionChanged',
     'OnLockedItemChangeUI']

    def ProcessSessionChange(self, isRemote, session, change):
        if ((not self.destroyed) and (self.sr and self.sr.node)):
            if (('shipid' in change) and (getattr(self, 'isShip', 0) and (self and (not self.destroyed)))):
                self.Load(self.sr.node)



    def OnSessionChanged(self, isRemote, session, change):
        if ((not self.destroyed) and (self.sr and self.sr.node)):
            if (('shipid' in change) and (getattr(self, 'isShip', 0) and (self and (not self.destroyed)))):
                self.Load(self.sr.node)



    def OnLockedItemChangeUI(self, itemID, ownerID, locationID, change):
        if ((not self.destroyed) and self.sr.node):
            if (itemID == self.id):
                item = None
                if self.rec:
                    item = self.rec
                elif self.sr.node.item:
                    item = self.sr.node.item
                if (item is None):
                    uix.LogInfo('Lock issue item is None')
                else:
                    locked = ((item.flagID == const.flagLocked) or sm.GetService('corp').IsItemLocked(item))
                    uix.LogInfo('Locked:', locked, 'item:', item)
                    self.SetLockState(locked)



    def init(self):
        self.typeID = None
        self.subTypeID = None
        self.id = None
        self.powerType = None
        self.sr.quantity_label = uicls.Label(text='', parent=uiutil.GetChild(self, 'qtypar'), left=4, top=1, letterspace=1, fontsize=9, state=uiconst.UI_DISABLED, idx=0, singleline=1)
        self.sr.quantity_label.parent.state = uix.UI_HIDDEN
        self.sr.name_label = self.sr.label = uicls.Label(text='', parent=self, left=0, top=66, width=64, autowidth=False, state=uiconst.UI_DISABLED, idx=3, maxlines=2)
        self.sr.icon = uicls.Icon(uiutil.GetChild(self, 'pic'))
        self.sr.container_icon = uiutil.GetChild(self, 'container')
        self.sr.contraband_icon = uicls.Icon(uiutil.GetChild(self, 'contrabandIcon'))
        self.sr.contraband_icon.sr.hint = mls.UI_SHARED_THISITEMISCONTRABAND
        self.sr.contraband_icon.state = uix.UI_HIDDEN
        self.sr.slotsize_icon = uicls.Icon(uiutil.GetChild(self, 'slotSize'))
        self.sr.slotsize_icon.sr.hint = mls.UI_SHARED_FITTINGCONSTRAINTS
        self.sr.slotsize_icon.state = uix.UI_HIDDEN
        self.sr.temp = uiutil.GetChild(self, 'temp')
        self.sr.ammosize_icon = uicls.Icon(uiutil.GetChild(self, 'ammoSize'))
        self.sr.ammosize_icon.sr.hint = mls.UI_SHARED_AMMOSIZECONSTRAINT
        self.sr.ammosize_icon.state = uix.UI_HIDDEN
        self.sr.topoverlays = uiutil.GetChild(self, 'topoverlays')
        self.sr.flags = uiutil.GetChild(self, 'flags')
        self.sr.dot = uiutil.GetChild(self, 'dot')
        self.sr.shadow = uiutil.GetChild(self, 'shadow')
        self.sr.subicon = uiutil.GetChild(self, 'subicon')
        self.sr.node = None
        self.sr.tlicon = None
        self.sr.selection = None
        self.sr.hilite = None
        self.highlightable = True
        sm.RegisterNotify(self)



    def SetState(self, state):
        self.viewOnly = self.Draggable_blockDrag = state
        if self.sr.node:
            self.sr.node.viewOnly = self.sr.node.Draggable_blockDrag = state



    def SetLockState(self, locked):
        self.SetState(min(1, locked))
        self.sr.icon.color.a = [1.0, 0.25][min(1, locked)]



    def Reset(self):
        self.viewOnly = 0
        self.subTypeID = None
        self.sr.ammosize_icon.state = uix.UI_HIDDEN
        self.sr.slotsize_icon.state = uix.UI_HIDDEN
        self.sr.container_icon.state = uix.UI_HIDDEN
        self.sr.subicon.state = uix.UI_HIDDEN
        self.sr.icon.state = uix.UI_HIDDEN



    def PreLoad(node):
        if (node.viewMode in ('list', 'details')):
            label = uix.GetItemLabel(node.item, node)



    def Load(self, node):
        self.sr.node = node
        data = node
        self.sr.node.__guid__ = self.__guid__
        self.sr.node.itemID = node.item.itemID
        self.id = node.item.itemID
        self.rec = node.item
        self.typeID = node.item.typeID
        self.isShip = ((self.rec.categoryID == const.categoryShip) and self.rec.singleton)
        self.isStation = ((self.rec.categoryID == const.categoryStation) and (self.rec.groupID == const.groupStation))
        self.isContainer = ((self.rec.groupID in (const.groupWreck,
         const.groupCargoContainer,
         const.groupSecureCargoContainer,
         const.groupAuditLogSecureContainer,
         const.groupFreightContainer)) and self.rec.singleton)
        self.isHardware = node.invtype.Group().Category().IsHardware()
        self.sr.node.isBlueprint = (node.invtype.Group().categoryID == const.categoryBlueprint)
        if self.sr.node.isBlueprint:
            self.sr.node.isCopy = (self.sr.node.isBlueprint and (self.rec.singleton == const.singletonBlueprintCopy))
        if (self.sr.node is None):
            return 
        uix.Flush(self.sr.temp)
        self.Reset()
        self.name = uix.GetItemName(node.item, self.sr.node)
        self.quantity = self.rec.stacksize
        listFlag = (self.sr.node.viewMode in ('list', 'details'))
        if (eve.session.shipid == self.sr.node.item.itemID):
            left = [-7, -4][listFlag]
            top = [-7, 1][listFlag]
            f = uicls.Frame(parent=self.sr.temp, idx=0, frameConst=uiconst.FRAME_BORDER2_CORNER0)
            f.SetPadding(left, top, left, top)
        if self.sr.node.Get('selected', 0):
            self.Select()
        else:
            self.Deselect()
        attribs = node.Get('godmaattribs', {})
        self.powerType = None
        if self.isHardware:
            if (self.sr.node.viewMode != 'list'):
                if attribs.has_key(const.attributeChargeSize):
                    self.sr.ammosize_icon.rectLeft = [0,
                     16,
                     32,
                     48,
                     64][(int(attribs[const.attributeChargeSize]) - 1)]
                    self.sr.ammosize_icon.state = uix.UI_DISABLED
                else:
                    if attribs.has_key(const.attributeRigSize):
                        self.sr.ammosize_icon.rectLeft = [0,
                         16,
                         32,
                         48,
                         64][(int(attribs[const.attributeRigSize]) - 1)]
                        self.sr.ammosize_icon.state = uix.UI_DISABLED
            for effect in cfg.dgmtypeeffects.get(self.rec.typeID, []):
                if (effect.effectID in (const.effectRigSlot,
                 const.effectHiPower,
                 const.effectMedPower,
                 const.effectLoPower)):
                    if (self.sr.node.viewMode != 'list'):
                        effinfo = cfg.dgmeffects.Get(effect.effectID)
                        iconNo = {const.effectRigSlot: 'ui_38_16_124',
                         const.effectHiPower: 'ui_38_16_123',
                         const.effectMedPower: 'ui_38_16_122',
                         const.effectLoPower: 'ui_38_16_121'}[effect.effectID]
                        self.sr.slotsize_icon.ChangeIcon(icon=iconNo, size=16, ignoreSize=True)
                        self.sr.slotsize_icon.state = uix.UI_DISABLED
                    self.powerType = effect.effectID
                    continue
                if ((self.sr.node.viewMode != 'list') and ((effect.effectID == const.effectSubSystem) and (const.attributeSubSystemSlot in attribs))):
                    subsystemFlag = attribs.get(const.attributeSubSystemSlot, None)
                    iconNo = 'ui_38_16_42'
                    self.sr.slotsize_icon.ChangeIcon(icon=iconNo, size=32, ignoreSize=True)
                    self.sr.slotsize_icon.state = uix.UI_DISABLED

        elif (self.rec.groupID == const.groupVoucher):
            if (self.rec.typeID != const.typeBookmark):
                self.subTypeID = self.sr.node.voucher.GetTypeInfo()[1]
        elif ((self.rec.categoryID == const.categoryCharge) and attribs.has_key(const.attributeChargeSize)):
            self.sr.ammosize_icon.state = uix.UI_DISABLED
            self.sr.ammosize_icon.rectLeft = [0,
             16,
             32,
             48,
             64][(int(attribs[const.attributeChargeSize]) - 1)]
        self.sr.contraband_icon.state = (uix.UI_HIDDEN, uix.UI_DISABLED)[((0 < len(self.sr.node.invtype.Illegality())) and (self.sr.node.invtype.Illegality().get(sm.GetService('map').GetItem(eve.session.solarsystemid2).factionID, None) is not None))]
        if listFlag:
            self.align = uix.UI_ALTOP
            self.sr.name_label.top = 4
            self.sr.name_label.width = eve.triapp.uilib.desktop.width
        else:
            self.sr.name_label.top = 66
            self.sr.name_label.width = 64
        if (self.sr.node.viewMode in ('icons', 'details')):
            if (self.sr.node.viewMode == 'details'):
                self.sr.name_label.left = 46
                self.sr.icon.tripass = eve.rot.GetInstance('res:/UI/Pass/linear.blue')
                self.sr.topoverlays.align = uix.UI_ALNONE
                self.sr.topoverlays.left = 5
                self.sr.topoverlays.top = 3
                self.sr.topoverlays.width = self.sr.topoverlays.height = 32
                self.sr.flags.top = 16
            else:
                self.sr.icon.tripass = None
                self.sr.topoverlays.align = uix.UI_ALCLIENT
                self.sr.topoverlays.left = 1
                self.sr.topoverlays.top = 2
                self.sr.topoverlays.width = 1
                self.sr.topoverlays.height = 4
            self.sr.icon.ChangeIcon(typeID=self.rec.typeID, ignoreSize=True, isCopy=(self.sr.node.isBlueprint and self.sr.node.isCopy))
            if (self.sr.node.viewMode == 'icons'):
                self.sr.icon.SetSize(64, 64)
            else:
                self.sr.icon.SetSize(32, 32)
            self.sr.subicon.texture = self.sr.icon.texture
            self.sr.subicon.rectLeft = self.sr.icon.rectLeft
            self.sr.subicon.rectTop = self.sr.icon.rectTop
            self.sr.subicon.rectWidth = 32
            self.sr.subicon.rectHeight = 32
            self.sr.subicon.tripass = eve.rot.GetInstance('res:/UI/Pass/linear.blue')
            self.sr.icon.state = uix.UI_DISABLED
            self.sr.dot.state = uix.UI_DISABLED
            self.sr.shadow.state = uix.UI_DISABLED
        else:
            self.sr.ammosize_icon.state = uiconst.UI_HIDDEN
            self.sr.flags.top = 0
            self.sr.icon.state = uix.UI_HIDDEN
            self.sr.subicon.state = uix.UI_HIDDEN
            self.sr.dot.state = uix.UI_HIDDEN
            self.sr.shadow.state = uix.UI_HIDDEN
            self.sr.name_label.left = 12
        self.UpdateLabel()
        self.LoadTechLevelIcon(node.item.typeID)
        locked = node.Get('locked', 0)
        viewOnly = node.Get('viewOnly', 0)
        self.SetLockState(locked)
        if not locked:
            self.SetState(viewOnly)
        if self.isStation:
            self.Draggable_blockDrag = 1
        elif node.Get('Draggable_blockDrag', None):
            self.Draggable_blockDrag = node.Draggable_blockDrag



    def LoadTechLevelIcon(self, typeID = None):
        offset = [-1, 0][(self.sr.node.viewMode == 'details')]
        tlicon = uix.GetTechLevelIcon(self.sr.tlicon, offset, typeID)
        if ((tlicon is not None) and (util.GetAttrs(tlicon, 'parent') is None)):
            self.sr.tlicon = tlicon
            self.sr.topoverlays.children.insert(0, tlicon)



    def UpdateLabel(self, new = 0):
        label = uix.GetItemLabel(self.rec, self.sr.node, new)
        if (self.sr.node.viewMode in ('list', 'details')):
            self.sr.label.singleline = 1
            self.sr.label.text = label
            self.sr.quantity_label.parent.state = uix.UI_HIDDEN
        else:
            self.sr.label.singleline = 0
            self.sr.name_label.text = label
            if (self.rec.singleton or (self.rec.typeID in (const.typeBookmark))):
                self.sr.quantity_label.parent.state = uix.UI_HIDDEN
            else:
                self.sr.quantity_label.text = ('%s' % uix.GetItemQty(self.sr.node, 'ss'))
                self.sr.quantity_label.parent.state = uix.UI_DISABLED



    def GetMenu(self):
        if self.sr.node:
            containerMenu = []
            if hasattr(self.sr.node.scroll.sr.content, 'GetMenu'):
                containerMenu = self.sr.node.scroll.sr.content.GetMenu()
            selected = self.sr.node.scroll.GetSelectedNodes(self.sr.node)
            args = []
            for node in selected:
                if node.item:
                    args.append((node.item,
                     node.Get('viewOnly', 0),
                     node.Get('voucher', None)))

            return ((sm.GetService('menu').InvItemMenu(args) + [None]) + containerMenu)
        else:
            return sm.GetService('menu').InvItemMenu(self.rec, self.viewOnly)



    def GetHeight(self, *args):
        (node, width,) = args
        if (node.viewMode in ('details', 'assets')):
            node.height = 42
        else:
            node.height = 21
        return node.height



    def Select(self):
        if (self.sr.node is not None):
            listFlag = (self.sr.node.viewMode in ('list', 'details'))
            left = [-3, 0][listFlag]
            top = [-3, 1][listFlag]
            if (self.sr.selection is None):
                self.sr.selection = uicls.Fill(parent=self, align=uiconst.TOALL, left=left, top=top, width=left, height=top)
            else:
                self.sr.selection.left = left
                self.sr.selection.top = top
            self.sr.selection.state = uiconst.UI_DISABLED



    def Deselect(self):
        if self.sr.selection:
            self.sr.selection.state = uix.UI_HIDDEN



    def _OnClick(self, *args):
        if self.sr.node:
            if self.sr.node.Get('OnClick', None):
                self.sr.node.OnClick(self)
            else:
                self.sr.node.scroll.SelectNode(self.sr.node)
                eve.Message('ListEntryClick')



    def _OnMouseEnter(self, *args):
        if uicore.uilib.leftbtn:
            return 
        self.sr.hint = ''
        wnd = sm.GetService('window').GetWindow('fitting')
        if (wnd is not None):
            if getattr(self, 'rec', None):
                wnd.HiliteFitting(self.rec)
        if self.sr.node:
            if (self.sr.node.viewMode == 'icons'):
                self.sr.hint = ('%s%s' % ([(uix.GetItemQty(self.sr.node, 'ln') + ' - '), ''][bool(self.rec.singleton)], uix.GetItemName(self.sr.node.item, self.sr.node)))
            else:
                eve.Message('ListEntryEnter')
            self.Hilite()



    def Hilite(self):
        if not self.highlightable:
            return 
        listFlag = (self.sr.node.viewMode in ('list', 'details'))
        left = [-3, 0][listFlag]
        top = [-3, 1][listFlag]
        if (self.sr.hilite is None):
            self.sr.hilite = uicls.Fill(parent=self, align=uiconst.TOALL, left=left, top=top, width=left, height=top)
        else:
            self.sr.hilite.left = left
            self.sr.hilite.top = top
        self.sr.hilite.state = uiconst.UI_DISABLED



    def Lolite(self):
        if self.sr.hilite:
            self.sr.hilite.state = uiconst.UI_HIDDEN



    def _OnMouseExit(self, *args):
        self.Lolite()
        if getattr(self, 'Draggable_dragging', 0):
            return 
        wnd = sm.GetService('window').GetWindow('fitting')
        if (wnd is not None):
            wnd.HiliteFitting(None)



    def _OnDblClick(self, *args):
        if (self.sr.node and self.sr.node.Get('OnDblClick', None)):
            self.sr.node.OnDblClick(self)
        elif not self.viewOnly:
            if self.isShip:
                self.OpenCargo()
            else:
                if self.isContainer:
                    self.OpenContainer()



    def _OnMouseDown(self, *args):
        if getattr(self, 'powerType', None):
            wnd = sm.GetService('window').GetWindow('fitting')
            if (wnd is not None):
                wnd.HiliteFitting(self.rec)
        uicls.Container._OnMouseDown(self, *args)



    def GetDragData(self, *args):
        nodes = self.sr.node.scroll.GetSelectedNodes(self.sr.node)
        return nodes



    def _OnMouseUp(self, btn, *args):
        if (eve.triapp.uilib.mouseOver != self):
            if getattr(self, 'powerType', None):
                main = sm.GetService('station').GetSvc('fitting')
                if (main is not None):
                    main.Hilite(None)
        uicls.Container._OnMouseUp(self, btn, *args)



    def OpenCargo(self):
        if not (self.rec.ownerID == eve.session.charid):
            eve.Message('CantDoThatWithSomeoneElsesStuff')
            return 
        name = self.sr.node.name
        if sm.StartService('menu').CheckSameLocation(self.rec)[0]:
            sm.GetService('window').OpenCargo(self.rec.itemID, name, ("%s's cargo" % name), self.rec.typeID)



    def OpenContainer(self):
        if (self.rec.ownerID not in (eve.session.charid, eve.session.corpid)):
            eve.Message('CantDoThatWithSomeoneElsesStuff')
            return 
        name = self.sr.node.name
        if (self.rec.typeID == const.typePlasticWrap):
            sm.GetService('window').OpenContainer(self.rec.itemID, name, ("%s's container" % name), self.rec.typeID, hasCapacity=0)
        elif sm.StartService('menu').CheckSameLocation(self.rec)[0]:
            sm.GetService('window').OpenContainer(self.rec.itemID, name, ("%s's container" % name), self.rec.typeID, hasCapacity=1)
        else:
            location = self.rec.locationID
            if ((not session.stationid) or (util.IsStation(location) and (location != session.stationid))):
                uix.LogInfo('Trying to open a container in', location, 'while actor is in', session.stationid)
                return 
            inventory = eve.GetInventoryFromId(location)
            if not inventory:
                return 
            item = inventory.GetItem()
            if not item:
                return 
            category = getattr(item, 'categoryID', None)
            if ((category == const.categoryShip) and (item.locationID == session.stationid)):
                sm.GetService('window').OpenContainer(self.rec.itemID, name, ("%s's container" % name), self.rec.typeID, hasCapacity=1)



    def OnDropData(self, dragObj, nodes):
        if (len(nodes) and nodes[0].scroll):
            nodes[0].scroll.ClearSelection()
            if not nodes[0].rec:
                return 
            if ((not hasattr(nodes[0].rec, 'locationID')) or (not sm.GetService('consider').ConfirmTakeFromContainer(nodes[0].rec.locationID))):
                return 
        mergeToMe = []
        notUsed = []
        addToShip = []
        addToContainer = []
        sourceID = None
        for node in nodes:
            if (node.Get('__guid__', None) not in ('xtriui.InvItem', 'listentry.InvItem', 'listentry.InvFittingItem')):
                notUsed.append(node)
                continue
            if (node.item.itemID == self.sr.node.item.itemID):
                notUsed.append(node)
                continue
            if cfg.IsContainer(self.sr.node.item):
                if (node.Get('__guid__', None) in ('xtriui.InvItem', 'listentry.InvItem')):
                    addToContainer.append(node.itemID)
            if self.isShip:
                addToShip.append(node.item.itemID)
            elif ((node.item.typeID == self.sr.node.item.typeID) and (not isinstance(self.sr.node.item.itemID, tuple))):
                mergeToMe.append(node.item)
            else:
                notUsed.append(node)
            if (sourceID is None):
                sourceID = node.rec.locationID

        if (sourceID is None):
            uix.LogInfo('OnDropData: Moot operation with ', nodes)
            return 
        if (self.isShip and addToShip):
            if ((self.rec.ownerID != session.charid) and (eve.Message('ConfirmOneWayItemMove', {}, uix.OKCANCEL) != uix.ID_OK)):
                return 
            ship = eve.GetInventoryFromId(self.rec.itemID)
            if ship:
                ship.MultiAdd(addToShip, sourceID, flag=const.flagCargo)
        shift = eve.triapp.uilib.Key(uix.VK_SHIFT)
        mergeData = []
        stateMgr = sm.StartService('godma').GetStateManager()
        singletons = []
        for invItem in mergeToMe:
            if (invItem.stacksize == 1):
                quantity = 1
            elif shift:
                ret = uix.QtyPopup(invItem.stacksize, 1, 1, None, 'Stack items')
                if (ret is not None):
                    quantity = ret['qty']
                else:
                    quantity = None
            else:
                quantity = invItem.stacksize
            if not quantity:
                continue
            if (quantity > 0):
                mergeData.append((invItem.itemID,
                 self.rec.itemID,
                 quantity,
                 invItem))
            if (type(invItem.itemID) is tuple):
                flag = invItem.itemID[1]
                chargeIDs = stateMgr.GetSubLocationsInBank(invItem.itemID)
                if chargeIDs:
                    for chargeID in chargeIDs:
                        charge = stateMgr.GetItem(chargeID)
                        if (charge.flagID == flag):
                            continue
                        mergeData.append((charge.itemID,
                         self.rec.itemID,
                         charge.stacksize,
                         charge))

            else:
                crystalIDs = stateMgr.GetCrystalsInBank(invItem.itemID)
                if crystalIDs:
                    for crystalID in crystalIDs:
                        if (crystalID == invItem.itemID):
                            continue
                        crystal = stateMgr.GetItem(crystalID)
                        if crystal.singleton:
                            singletons.append(crystalID)
                        else:
                            mergeData.append((crystal.itemID,
                             self.rec.itemID,
                             crystal.stacksize,
                             crystal))


        if (singletons and util.GetAttrs(self, 'sr', 'node', 'rec', 'flagID')):
            flag = self.sr.node.rec.flagID
            inv = eve.GetInventoryFromId(self.rec.locationID)
            if inv:
                inv.MultiAdd(singletons, sourceID, flag=flag, fromManyFlags=True)
        if (mergeData and util.GetAttrs(self, 'sr', 'node', 'container', 'MultiMerge')):
            self.sr.node.container.MultiMerge(mergeData, sourceID)
        if addToContainer:
            flag = settings.user.ui.Get(('defaultContainerLock_%s' % self.sr.node.item.itemID), None)
            if (flag is None):
                flag = const.flagLocked
            eve.GetInventoryFromId(self.sr.node.item.itemID).MultiAdd(addToContainer, sourceID, flag=flag)
        elif (notUsed and util.GetAttrs(self, 'sr', 'node', 'container', 'OnDropData')):
            self.sr.node.container.OnDropData(dragObj, notUsed)




class Item(InvItem):
    __guid__ = 'listentry.InvItem'
    __resPath__ = 'res:/UI/Component/icon_listversion.blue'

    def init(self):
        xtriui.InvItem.init(self)
        uicls.Line(parent=self, align=uiconst.TOBOTTOM, idx=1)




class InvBlueprintItem(Item):
    __guid__ = 'listentry.InvBlueprintItem'

    def UpdateLabel(self, new = 0):
        xtriui.InvItem.UpdateLabel(self, new)
        blueprint = self.sr.node.blueprint
        labeltext = self.sr.label.text
        isCopy = [mls.UI_GENERIC_NO, mls.UI_GENERIC_YES][blueprint.copy]
        ml = blueprint.materialLevel
        pl = blueprint.productivityLevel
        lprr = blueprint.licensedProductionRunsRemaining
        if (lprr == -1):
            lprr = ''
        label = ('<t>%s<t>%s<t>%s<t>%s' % (isCopy,
         ml,
         pl,
         lprr))
        if (self.sr.node.viewMode in ('list', 'details')):
            self.sr.label.text += label
            label = self.sr.label.text
        else:
            self.sr.name_label.text += label
            label = self.sr.name_label.text
        self.sr.node.label = label




class ItemWithVolume(Item):
    __guid__ = 'listentry.InvItemWithVolume'

    def UpdateLabel(self, new = 0):
        xtriui.InvItem.UpdateLabel(self, new)
        if util.GetAttrs(self, 'sr', 'node', 'remote'):
            return 
        volume = cfg.GetItemVolume(self.rec)
        self.sr.node.Set(('sort_%s' % mls.UI_GENERIC_VOLUME), volume)
        unit = Tr(cfg.eveunits.Get(const.unitVolume).displayName, 'dbo.eveUnits.displayName', const.unitVolume)
        label = ('<t>%s %s' % (util.FmtAmt(volume), unit))
        if (self.sr.node.viewMode in ('list', 'details')):
            self.sr.label.text += label
            label = self.sr.label.text
        else:
            self.sr.name_label.text += label
            label = self.sr.name_label.text
        self.sr.node.label = label




class ItemCheckbox(Item):
    __guid__ = 'listentry.ItemCheckbox'

    def Startup(self, *args):
        cbox = uicls.Checkbox(align=uiconst.TOPLEFT, pos=(4, 4, 0, 0), callback=self.CheckBoxChange)
        cbox.data = {}
        self.children.insert(0, cbox)
        self.sr.checkbox = cbox
        self.sr.checkbox.state = uix.UI_DISABLED
        self.sr.dot.left = 20
        self.sr.shadow.left = 16
        self.sr.subicon.state = uix.UI_HIDDEN
        self.sr.icon.left = 20
        self.sr.flags.left = -18
        self.sr.flags.top = 18



    def Load(self, args):
        xtriui.InvItem.Load(self, args)
        if (self.sr.tlicon is not None):
            self.sr.tlicon.left = 16
            self.sr.tlicon.top = 0
        self.sr.label.left = ((self.sr.icon.left + self.sr.icon.width) + 6)
        data = self.sr.node
        self.sr.checkbox.SetGroup(data.group)
        self.sr.checkbox.SetChecked(data.checked, 0)
        self.sr.checkbox.data.update({'key': data.cfgname,
         'retval': data.retval})
        if not data.OnChange:
            data.OnChange = self.OnChange
        if self.sr.tlicon:
            self.sr.tlicon.left += 1
            self.sr.tlicon.top += 2



    def OnChange(self, checkbox):
        return 



    def CheckBoxChange(self, *args):
        self.sr.node.checked = self.sr.checkbox.checked
        self.sr.node.OnChange(*args)



    def _OnClick(self, *args):
        if self.sr.checkbox.checked:
            eve.Message('DiodeDeselect')
        else:
            eve.Message('DiodeClick')
        if (self.sr.checkbox.groupName is None):
            self.sr.checkbox.SetChecked((not self.sr.checkbox.checked))
            return 
        for node in self.sr.node.scroll.GetNodes():
            if ((node.Get('__guid__', None) == 'listentry.Checkbox') and (node.Get('group', None) == self.sr.checkbox.groupName)):
                if node.panel:
                    node.panel.sr.checkbox.SetChecked(0, 0)
                    node.checked = 0
                else:
                    node.checked = 0

        if not self.destroyed:
            self.sr.checkbox.SetChecked(1)




class InvBlueprintCheckbox(ItemCheckbox):
    __guid__ = 'listentry.InvBlueprintCheckbox'

    def UpdateLabel(self, new = 0):
        xtriui.InvItem.UpdateLabel(self, new)
        hideColumns = self.sr.node.Get('hideColumns', [])
        blueprint = self.sr.node.blueprint
        label = ''
        if (mls.UI_GENERIC_COPY not in hideColumns):
            isCopy = [mls.UI_GENERIC_NO, mls.UI_GENERIC_YES][blueprint.copy]
            self.sr.node.Set(('sort_%s' % mls.UI_GENERIC_COPY), isCopy)
            label += ('<t>%s' % isCopy)
        if (mls.UI_RMR_ML not in hideColumns):
            ml = blueprint.materialLevel
            self.sr.node.Set(('sort_%s' % mls.UI_RMR_ML), ml)
            label += ('<t>%s' % ml)
        if (mls.UI_RMR_PL not in hideColumns):
            pl = blueprint.productivityLevel
            self.sr.node.Set(('sort_%s' % mls.UI_RMR_PL), pl)
            label += ('<t>%s' % pl)
        if (mls.UI_GENERIC_RUNS not in hideColumns):
            lprr = blueprint.licensedProductionRunsRemaining
            if (lprr == -1):
                lprr = ''
            self.sr.node.Set(('sort_%s' % mls.UI_GENERIC_RUNS), lprr)
            label += ('<t>%s' % lprr)
        if (self.sr.node.viewMode in ('list', 'details')):
            if (mls.UI_GENERIC_QTY in hideColumns):
                self.sr.label.text = self.sr.label.text.replace('<right><t>', '')
            self.sr.label.text += label
            label = self.sr.label.text
        elif (mls.UI_GENERIC_QTY in hideColumns):
            self.sr.name_label.text = self.sr.name_label.text.replace('<right><t>', '')
        self.sr.name_label.text += label
        label = self.sr.name_label.text
        self.sr.node.label = label




class InvAssetItem(Item):
    __guid__ = 'listentry.InvAssetItem'

    def OnDropData(self, dragObj, nodes):
        return 




class InvFittingItem(InvItem):
    __guid__ = 'listentry.InvFittingItem'
    __resPath__ = 'res:/UI/Component/icon_listversion.blue'

    def init(self):
        xtriui.InvItem.init(self)
        uicls.Line(parent=self, align=uiconst.TOBOTTOM, idx=1)
        self.sr.fitIcon = uicls.Icon(icon='ui_38_16_184', parent=self, size=16, idx=0)
        self.sr.fitIcon.anchors = (uix.UI_ANCHRIGHT | uix.UI_ANCHBOTTOM)
        self.sr.fitIcon.OnClick = self.FitToActiveOrUnfit
        self.sr.fitIcon.hint = mls.UI_CMD_FITTOACTIVESHIP
        self.sr.fitIcon._OnMouseEnter = self._OnMouseEnter
        self.sr.fitIcon._OnMouseExit = self._OnMouseExit
        self.sr.fitIcon.top = 4



    def UpdateLabel(self, new = 0):
        name = uix.GetItemName(self.rec, self.sr.node)
        self.sr.label.text = (name + ('<br>%s: %s' % (mls.UI_GENERIC_QTY, (uix.GetItemQty(self.sr.node, 'ss') or 1))))
        self.sr.quantity_label.parent.state = uix.UI_HIDDEN
        if (self.sr.node.Get('showFitIcon', 1) and (self.isHardware or (self.sr.node.invtype.Group().Category().id == const.categoryCharge))):
            if (self.rec.flagID not in (const.flagHangar, const.flagCargo)):
                uix.MapSprite('38_184', self.sr.fitIcon)
                self.sr.fitIcon.hint = mls.UI_CMD_REMOVE
                self.sr.fitIcon.state = uix.UI_NORMAL
            else:
                if (self.rec.flagID in (const.flagDroneBay)):
                    self.sr.fitIcon.state = uix.UI_HIDDEN
        else:
            self.sr.fitIcon.state = uix.UI_HIDDEN



    def FitToActiveOrUnfit(self, *args):
        selected = self.sr.node.scroll.GetSelectedNodes(self.sr.node)
        if selected:
            args = []
            for node in selected:
                if node.item:
                    args.append(node.item)

            if (self.sr.node.item not in args):
                args.append(self.sr.node.item)
        else:
            args = [self.sr.node.item]
        if (len(args) == 0):
            return 
        sourceLocationID = args[0].locationID
        if (self.rec.flagID not in (const.flagHangar, const.flagCargo)):
            if (eve.session.stationid is not None):
                eve.GetInventory(const.containerHangar).MultiAdd([ rec.itemID for rec in args ], sourceLocationID, flag=const.flagHangar)
            else:
                eve.GetInventoryFromId(eve.session.shipid).MultiAdd([ rec.itemID for rec in args ], sourceLocationID, flag=const.flagCargo)
        else:
            sm.GetService('menu').TryFit(args)




